﻿using System;
using System.IO;
using Renci.SshNet;
using System.Diagnostics;

/*
 Requirements
Implement the SFTP Client Library.
Implement basic methods (Connect, Disconnect, MakeDir, List, upload...etc).
Instead of developing a separate SFtp Client for the library, should rather merge both SFTP & FTP functionality in the same C# class.

 */

namespace SFTP_Renci_SshNet

{
    class Program
    {


        

        public void MakeDirectionInRemoteHost(string NameDirectionInRemoteHost, string RemoteDir, SftpClient sftp)
        {
            string choice = "";
            
            var list = sftp.ListDirectory(RemoteDir);
                               
                     
                foreach (var item in list)
                {
                    if (item.ToString() == NameDirectionInRemoteHost)
                    {
                          System.Diagnostics.Debug.WriteLine("\n\r directory " + NameDirectionInRemoteHost + " already exist \n\r");
                        choice = "exist";
                    }


                }
              
                if (choice != "exist")
                {
                    sftp.CreateDirectory(RemoteDir +"/" + NameDirectionInRemoteHost);
                }

            

        
        
        }

     

        static void Main(string[] args)
        {
            #region Definition

            System.Diagnostics.Debug.Listeners.Add(new ConsoleTraceListener());
        string strHost = "68.68.27.35";
        string strUserName = "yousfi";
        string strPwd = "C!rru$C@pture";
        int nPort = 22;
            //string dir = @"D:\My Programm\123";
            string choice = "";
            string establish = "";
            string RemoteDir = "/Yousfi";
            string DownloadFile = "DownloadFile";
            string NameDirectionInRemoteHost = "Download";
            SftpClient sftp = new SftpClient(strHost, nPort, strUserName, strPwd);

            string NameDirectionForDelete = "Norin";
           // System.Collections.ArrayList list;
           var dfgdfg = "";
            #endregion
           System.IO.Directory.CreateDirectory(@"Downloaded files/");
           System.IO.Directory.CreateDirectory(@"Files to upload/");

            try
            {

               #region Connect

               System.Diagnostics.Debug.WriteLine ("\n\r Connecting...");
                

                try
                {
                  
                    sftp.Connect();
                }
                catch (Exception e)
                {
                      System.Diagnostics.Debug.WriteLine("\n\rFailed connect to " + strHost);
                    Console.ReadLine();
                }
                
                  System.Diagnostics.Debug.WriteLine("OK");

                #endregion

                #region MKdir

              //    MakeDirectionInRemoteHost(RemoteDir, RemoteDir, sftp);
        
               
                #endregion

                #region Download


                  System.Diagnostics.Debug.WriteLine(" Download  all files");


                           

                if (sftp.IsConnected)
                {
                    var files = sftp.ListDirectory(RemoteDir);

                    foreach (var file in files)
                    {
                        if (!file.Name.StartsWith(".") & !file.IsDirectory)
                        {
                                System.Diagnostics.Debug.WriteLine(file.Name);


                            

                              Stream file1 = File.OpenWrite(@"Downloaded files/" + file.Name);
                              try{
                           

                            sftp.DownloadFile(file.FullName, file1);
                              }
                              catch (Exception ex) {   System.Diagnostics.Debug.WriteLine(ex.ToString()); }
                                                
                          

                        }

                    }
                }    

                    #endregion

                    #region Upload
                System.Diagnostics.Debug.WriteLine("-----------Start Method Upload");

                var list = sftp.ListDirectory(RemoteDir);
                
                foreach (var item in list)
                {
                    //System.Diagnostics.Debug.WriteLine(item.Name);

                    if (item.Name == NameDirectionInRemoteHost)
                    {
                        System.Diagnostics.Debug.WriteLine("\n\r directory " + NameDirectionInRemoteHost + " already exist \n\r");
                        choice = "exist";
                    }


                }

                if (choice != "exist")
                {
                    sftp.CreateDirectory(RemoteDir + "/" + NameDirectionInRemoteHost);
                }

                string[] Files = Directory.GetFiles(@"Files to upload");
               // string Files = System.IO.Path.GetFileName(@"Files to upload");
                //System.Diagnostics.Debug.WriteLine("--------" + Files);
                foreach (var item in Files)
                    {
                        int i = 1;
                        
                        FileInfo f = new FileInfo(item.ToString());
                       //  System.Diagnostics.Debug.WriteLine("+++++++"+f.Name);
                   
                            if (sftp.IsConnected)
                            {

                                try
                                {
                                    System.Diagnostics.Debug.WriteLine(RemoteDir + "/" + NameDirectionInRemoteHost + "/" + f.Name);
                                    sftp.ChangeDirectory(RemoteDir + "/" + NameDirectionInRemoteHost);
                                    var uplfileStream = System.IO.File.OpenRead(item.ToString());
                                    sftp.UploadFile(uplfileStream, f.Name, true);
                                  
                                    i++;

                                }
                                catch (Exception ec) { System.Diagnostics.Debug.WriteLine(ec.ToString()); }

                            }
                     }
                #endregion

                #region DeletPath

              list = sftp.ListDirectory(RemoteDir);

                foreach (var item in list)
                {
                    
                    if (item.Name == NameDirectionForDelete)
                    {
                        try
                        {
                            sftp.Delete(RemoteDir + "/" + NameDirectionForDelete);
                        }


                        catch (Exception ec) { System.Diagnostics.Debug.WriteLine(ec.ToString()); }

                        System.Diagnostics.Debug.WriteLine("\n\r directory " + NameDirectionForDelete + "  Delete \n\r");

                    }
                  
                }

                              

               

                #endregion

                #region Disconnect


                sftp.Disconnect();
                    sftp.Dispose();
                    
                #endregion

                    Console.ReadLine();

            }
            catch (Exception ex) 
            { 
                  System.Diagnostics.Debug.WriteLine(ex.ToString());
            Console.ReadLine();
           
            }
        
        }

       /* private static void Mkdir(SftpClient sftp)
        {
            throw new NotImplementedException();
        }*/
    }
}
